<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-19 14:00
 *
 * Version: 2.0
 */

namespace App\Modules\Employee\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;
use Illuminate\Support\Facades\DB;

class GetWxBarberInfoCriteria extends BaseCriterias
{
    private $params;

    public function __construct($params)
    {
        $this->params = $params;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $store_id = $this->params['store_id'];
        $barber_id = $this->params['barber_id'];
        $where = [
            'user_store.uid' => $this->params['barber_id'],
            'sid' => $this->params['store_id'],
        ];
        $select = [
            'hx_user.id',
            'hx_user.username',
            'hx_user.mobile',
            'hx_user.nick',
            'hx_picture.image as userPortraitUrl',
            'hx_video.url as video',
            'hx_video.thumb as thumb',
            'hx_store.name as store_name',
            'address',
            'doorplate',
            DB::raw('
					count(hx_user_like.id) AS likeNum,
					count(hx_order.id) as OrderNum
				')
        ];
        $select = implode($select, ',');
        $model = $model->selectRaw($select)
            ->leftJoin('user', 'user_store.uid', 'user.id')
            ->leftJoin('store', 'user_store.sid', 'store.id')
            ->leftJoin('picture', 'user.portrait', 'picture.id')
            ->leftJoin('video', 'user.profile_video', 'video.id')
            ->leftJoin('order',function ($v) use($store_id){
            	$v->on('order.barber_id','=','user_store.uid')->where(['order.status'=>2,'order.store_id'=>$store_id]);
            })
            ->leftJoin('user_like', function ($v) use($store_id){
            	$v->on('user_store.uid', '=','user_like.user_id')->where(['user_like.store_id'=>$store_id]);
            })
            ->where($where);
        return $model;
    }
}